<template>
    <!-- 编辑弹窗 -->
    <el-dialog v-model="store.state.index.AddForm" title="添加商品" class="animate__animated animate__slideInUp">
        <el-form ref="ruleFormRef" :model="addDity">
            <el-form-item label="商品名称" :label-width="formLabelWidth">
                <el-input v-model="addDity.goodsName" autocomplete="off" />
            </el-form-item>
            <el-form-item label="价格" :label-width="formLabelWidth">
                <el-input v-model="addDity.price" autocomplete="off" />
            </el-form-item>
            <el-form-item label="成分" :label-width="formLabelWidth">
                <el-input v-model="addDity.tagContent" autocomplete="off" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="handlecancel">取消</el-button>
                <el-button type="primary" @click="addubmit">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { defineEmits, reactive, ref } from 'vue'
import { useStore } from 'vuex';
import { ElMessage, FormInstance, } from 'element-plus'
import http from '../util/api'

const store = useStore()
const ruleFormRef = ref<FormInstance>()
const formLabelWidth = '140px'

const addDity = reactive<any>({
    id: '',
    goodsName: '',
    price: '',
    tagContent: ''
})
//父组件重新获取信息
const emit = defineEmits(['getList'])

//确认添加
const addubmit = (formEl: FormInstance | undefined) => {
    let data = {
        'goodsName':addDity.goodsName,
        'price': addDity.price,
        'tagContent': addDity.tagContent
    }
    http(`/production/`, 'POST',data).then((res: any) => {
        if (res.status === 201) {
            ElMessage({
                type: 'success',
                message: '添加成功！',
            })
            emit('getList')
            Object.keys(addDity).map(item => {
                delete addDity[item]
            })
        } else {
            ElMessage({
                type: 'error',
                message: '添加失败！',
            })
        }
        store.commit('Dialog')
    })

}
//取消修改
const handlecancel = () => {
    store.commit('changeDialog')
    ElMessage({
        type: 'info',
        message: '已取消！',
    })
}

</script>
<style scoped>
.el-button--text {
    margin-right: 15px;
}

.el-select {
    width: 300px;
}

.el-input {
    width: 300px;
}

.dialog-footer button:first-child {
    margin-right: 10px;
}
</style>